﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Answer = ELearningSystem.UI.AnswerServiceReference.AnswerDAO;
using AnswerManager = ELearningSystem.UI.AnswerServiceReference.AnswerServiceSoapClient;
using Content = ELearningSystem.UI.ContentServiceReference.ContentDAO;
using TreeContent = ELearningSystem.UI.ContentServiceReference.TreeContentDAO;
using Practice = ELearningSystem.UI.ContentServiceReference.PracticeDAO;
using ContentManager = ELearningSystem.UI.ContentServiceReference.ContentServiceSoapClient;


namespace ELearningSystem.UI.AdminUI
{
    public partial class AddPracticeUI : System.Web.UI.Page
    {
        private TreeContent treeContent;
        private ContentManager contentManager = new ContentManager();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                treeContent = (TreeContent)Session["Content"];
                topicLabel.Text = treeContent.ParentNode.Text;
                saveResponseLabel.Text = "";
                saveResponseLabel.ForeColor = Color.Green;
                saveResponseLabel.Text = Request.QueryString["message"];
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            if (questoinTextBox.Text != "" || answersTextBox.Text != "" || correctAnswersTextBox.Text != "" || titleTextBox.Text != "")
            {
                AnswerManager answerManager = new AnswerManager();
                List<Answer> answers =
                    new List<Answer>(
                        answerManager.GetAnswersFromAnswersStringAndCorrectAnswersString(answersTextBox.Text,
                                                                                         correctAnswersTextBox.Text));
                List<ContentServiceReference.AnswerDAO> practiceAnswers = new List<ContentServiceReference.AnswerDAO>();
                foreach (var answer in answers)
                {
                    ContentServiceReference.AnswerDAO practiceAnswer = new ContentServiceReference.AnswerDAO();
                    practiceAnswer.Id = answer.Id;
                    practiceAnswer.IsCorrect = answer.IsCorrect;
                    practiceAnswer.Text = answer.Text;
                    practiceAnswers.Add(practiceAnswer);
                }
                treeContent = (TreeContent) Session["Content"];
                treeContent.Text = titleTextBox.Text;
                Practice practice = new Practice();
                practice.Question = questoinTextBox.Text;
                practice.Answers = practiceAnswers;

                Content content = new Content();
                content.IsVideo = false;
                content.Practice = practice;


                treeContent.Content = content;
                bool isSaved = contentManager.SaveOrUpdate(treeContent);
                if (isSaved)
                {
                    Response.Redirect("~/AdminUI/AddPracticeUI.aspx?message=Successfully Saved & flag=true");
                }
                else
                {
                    saveResponseLabel.ForeColor = Color.Red;
                    saveResponseLabel.Text = "Saving Failed!!";
                }
            }
            else
            {
                saveResponseLabel.ForeColor = Color.Red;
                saveResponseLabel.Text = "Please fill-up all the fields properly!";
            }
        }

        protected void doneButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AdminUI/AdminHomePageUI.aspx");
        }
    }
}